import { Inject, Injectable } from "@nestjs/common"
import { Repository, IsNull ,Not, Raw } from "typeorm"
import { InjectRepository } from "@nestjs/typeorm"

import { Staff } from "./entities/my.entity"
import { Identity } from "./entities/identity.entity"
import { GetStaff, powerName ,staffDataInfo} from "./inteface/staffInteface"
import { utils } from "../config/config.utils"

@Injectable()
export class StaffService {
    constructor(
        @InjectRepository(Staff) private staff: Repository<Staff>,
        @InjectRepository(Identity) private identity: Repository<Identity>,
        @Inject("utils") private utils: utils
    ) { }



    //根据不同的权限获取不同的员工
    async getStaff(data: GetStaff) {
        // console.log(data)
        if (!data.take || !data.current || !data.power) {
            return {
                code: 400,
                mes: "你没有传分页的数据",
                data: [],
                count: 0
            }
        }
        //power=1 获取经理的数据
        //power=2获取项目负责人的数据
        // power=3获取组长的数据
        //power=4获取质检员的数据  
        //power=5获取内容审核员的数据
        let resData: object[] = []
        let power = parseInt(data.power)

        switch (true) {
            // 获取全部员工数据
            case power == 0:
                resData = await this.getAllDate(data);
                break
            case power == 7:
                // resData = await
                resData = await this.getOwnGroupMember(data);
                break
            default:
                resData = await this.getpowerData(data);
                break
        }
        // let count =  await this.staff.count({})
        // resData[0][0].registerTime = this.utils.dateConversion(resData.?registerTime) as unknown as Date

        return {

            code: 200,
            mes: "获取数据成功",
            data: resData[0],
            count: resData[1]
        }
    }

    // 根据员工的职位获取数据
    // 获取全部的数据
    async getAllDate(data: GetStaff): Promise<{}[]> {

        let skipNum = data.take * (data.current - 1)
        return Promise.resolve(
            await this.staff.createQueryBuilder("user").orderBy("user.registerTime", "DESC").
                leftJoinAndSelect("user.identity", "identity").skip(skipNum).take(data.take).getManyAndCount()

        )
    }

    //根据权限获取数据
    async getpowerData(data: GetStaff): Promise<{}[]> {
        let skipNum = data.take * (data.current - 1)
        return Promise.resolve(
            await this.staff.createQueryBuilder("user")
                .leftJoinAndSelect("user.identity", "identity").orderBy("user.registerTime", "DESC").
                where("identity.power=:power", { power: data.power }).
                skip(skipNum).take(data.take).getManyAndCount())
    }

    // //获取还没有设置职位的员工
    // async getStaffIsnull(data: GetStaff): Promise<{}[]> {
    //     let skipNum = data.take * (data.current - 1)
        
    //     let newArr = []
    //         // let res= await this.staff.createQueryBuilder("user")
    //         //     .leftJoinAndSelect("user.identity", "staff").
    //         //     orderBy("user.registerTime", "DESC").where({
    //         //         identity:IsNull()
    //         //     })
    //         //     .getManyAndCount()

    //         // return res
        
    //             // res[0].forEach(item=>{ 
    //     //         if(!item.identity){
    //     //             newArr.push(item)
    //     //         }

    //     // })

    //     // return [newArr,newArr.length]
    // }

    // 获取自己组的成功
    async getOwnGroupMember(data: GetStaff): Promise<{}[]> {
        let skipNum = data.take * (data.current - 1)
        return Promise.resolve(
            await this.staff.createQueryBuilder("user")
                .leftJoinAndSelect("user.identity", "identity").orderBy("user.registerTime", "DESC").
                where("identity.teamId=:teamId", { teamId: data.staff_id }).skip(skipNum).take(data.take).getManyAndCount())
    }

    // 获取可以选择组长的领导
    async getPower() {

        let res = await this.staff.createQueryBuilder("user").
            leftJoinAndSelect("user.identity", "identity").where("identity.power<:power", { power: 5 }).getMany()
        // console.log(res)

        let newData: powerName[] = []

        res.forEach((item) => {
            newData.push({
                staffId: item.staffId + "",
                staffName: item.staffName,
                job: item.identity.job
            })
        })
        newData.push({
            staffId: "0",
            staffName: "无",
            job: "0"
        })

        if (res.length > 0) {
            return {
                code: 200,
                mes: "获取数据成功",
                data: newData
            }
        } else {
            return {
                code: 200,
                mes: "获取数据失败",
                data: []
            }
        }
    }

    // 根据自己的员工Id获取自己组的员工
    async getOwnTeam(data:{staffId:string}){
        let newData:{staffName:string,staffId:number}[] = []
            // console.log(data)
          let resData = await this.staff.createQueryBuilder("user")
          .leftJoinAndSelect("user.identity","identity").
          where("identity.teamId=:staffId",{staffId:data.staffId}).getMany()
        //   console.log(resData)
        resData.forEach(item=>{
            let obj ={
                staffName:item.staffName,
                staffId:item.staffId
            }
            newData.push(obj)
        })

        if(resData.length>0){
            return {
                code:200,
                mes:"成功获取数据",
                data:newData
            }
        }else{
            return {
                code:400,
                mes:"获取数据失败",
                data:[]
            }
        }
      
        // this.staff

    }


}